package com.zy.springsessionredis.controller;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@SpringBootApplication
public class SpringSessionApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringSessionApplication.class, args);
    }

    @GetMapping("/set")
    public String setSession(HttpSession session) {
       // session.setAttribute("msg1", "Hello1");//session的数据放到redis,若session数据变化需要更新数据
        for (int i = 0; i < 18; i++) {
            session.setAttribute("msg" + i, "hello" + i);
            System.out.println("msg====="+i+"hello"+i);

        }
        return "ok";
    }

    @GetMapping("/get")
    public  Map<String,String> getSession(HttpSession session) {
        String attribute = "";
        List<String> list = new ArrayList<>();
       Map<String,String> map= new HashMap<String,String>();
        for (int i = 0; i < 8; i++) {
            attribute = (String) session.getAttribute("msg" + i);
            list.add(attribute);
            map.put("msg"+i,attribute);
            System.out.println(attribute);
        }
        return map;
    }
}
//我们可以看到，redis中保存了数据，而且如果我们设置多台服务器，访问get获取到的都是一样的